package com.cipe.cmrs.converter;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;

import com.cipe.cmrs.bean.AuditsBean;
import com.cipe.cmrs.bean.ImpBean;
import com.cipe.cmrs.model.Audits;
import com.cipe.cmrs.model.Imp;
import com.cipe.cmrs.service.AuditsService;
import com.cipe.cmrs.service.ImpService;

public class AuditsBeanToAudits implements Converter<AuditsBean, Audits> {

    private ConverterRegistry converterRegistry;
    private AuditsService auditsService;
    private ImpService impService;

    public void setConverterRegistry(ConverterRegistry converterRegistry) {
        this.converterRegistry = converterRegistry;
    }

    public void setAuditsService(AuditsService auditsService) {
        this.auditsService = auditsService;
    }

    public void setImpService(ImpService impService) {
        this.impService = impService;
    }

    public void init() {
        converterRegistry.addConverter(this);
    }

    public Audits convert(AuditsBean bean) {
        Audits audits = null;
        if (bean != null) {
            if (bean.getId() != null && bean.getId() > 0) {
                audits = auditsService.findById(bean.getId());
                audits.setUpdatedBy(bean.getUser());
                audits.setUpdationDate(new Date());
            } else {
                audits = new Audits();
                audits.setCreatedBy(bean.getUser());
                audits.setCreationDate(new Date());
            }

            audits.setNewgrantee(bean.isNewgrantee());
            audits.setRiskassessment(bean.getRiskassessment());
            audits.setSmponly(bean.isSmponly());
            audits.setInternalmonitoring(bean.isInternalmonitoring());
            audits.setInterimaudit(bean.isInterimaudit());
            audits.setFinalaudit(bean.isFinalaudit());

            // Internal Monitoring
            audits.setImprequired(bean.isImprequired());
            audits.setImpnotes(bean.getImpnotes());
            fillImps(audits, bean);

            // Interim Audit
            audits.setIasolicitor(bean.getIasolicitor());

            audits.setIaselectionmemodue(bean.getIaselectionmemodue());
            audits.setIaselectionmemoreceived(bean.getIaselectionmemoreceived());
            audits.setIaselectionmemoapproved(bean.getIaselectionmemoapproved());

            audits.setIafirm(bean.getIafirm());

            audits.setIadue(bean.getIadue());
            audits.setIareceived(bean.getIareceived());
            audits.setIaapproved(bean.getIaapproved());

            audits.setIarevisedresolutiondue(bean.getIarevisedresolutiondue());
            audits.setIarevisedresolutionreceived(bean.getIarevisedresolutionreceived());
            audits.setIarevisedresolutionapproved(bean.getIarevisedresolutionapproved());

            audits.setIarevisedresolutionoffindingsdue(bean.getIarevisedresolutionoffindingsdue());
            audits.setIarevisedresolutionoffindingsreceived(bean.getIarevisedresolutionoffindingsreceived());
            audits.setIarevisedresolutionoffindingsapproved(bean.getIarevisedresolutionoffindingsapproved());

            audits.setIaresolutionoffindingsdue(bean.getIaresolutionoffindingsdue());
            audits.setIaresolutionoffindingsreceived(bean.getIaresolutionoffindingsreceived());
            audits.setIaresolutionoffindingsapproved(bean.getIaresolutionoffindingsapproved());

            audits.setIamemonotes(bean.getIamemonotes());
            audits.setIaclosedtofile(bean.getIaclosedtofile());

            // Final Audit
            audits.setFasolicitor(bean.getFasolicitor());
            audits.setFaselectionmemoduedate(bean.getFaselectionmemoduedate());
            audits.setFaselectionmemodue(bean.getFaselectionmemodue());
            audits.setFaselectionmemoreceived(bean.getFaselectionmemoreceived());
            audits.setFaselectionmemoapproved(bean.getFaselectionmemoapproved());

            audits.setFafirm(bean.getFafirm());

            audits.setFadue(bean.getFadue());
            audits.setFareceived(bean.getFareceived());
            audits.setFaapproved(bean.getFaapproved());

            audits.setFarevisedresolutiondue(bean.getFarevisedresolutiondue());
            audits.setFarevisedresolutionreceived(bean.getFarevisedresolutionreceived());
            audits.setFarevisedresolutionapproved(bean.getFarevisedresolutionapproved());

            audits.setFarevisedresolutionoffindingsdue(bean.getFarevisedresolutionoffindingsdue());
            audits.setFarevisedresolutionoffindingsreceived(bean.getFarevisedresolutionoffindingsreceived());
            audits.setFarevisedresolutionoffindingsapproved(bean.getFarevisedresolutionoffindingsapproved());

            audits.setFaresolutionoffindingsdue(bean.getFaresolutionoffindingsdue());
            audits.setFaresolutionoffindingsreceived(bean.getFaresolutionoffindingsreceived());
            audits.setFaresolutionoffindingsapproved(bean.getFaresolutionoffindingsapproved());

            audits.setFamemonotes(bean.getFamemonotes());
            audits.setFaclosedtofile(bean.getFaclosedtofile());
        }
        return audits;
    }

    private void fillImps(Audits audits, AuditsBean bean) {
        Map<Integer, Imp> map = new HashMap<Integer, Imp>();
        List<Imp> list = audits.getImps();
        if (list == null) {
            list = new ArrayList<Imp>();
            audits.setImps(list);
        } else {
            for (Imp imp : list) {
                map.put(imp.getId(), imp);
            }
        }

        for (ImpBean impBean : bean.getImpBeans()) {
            Imp imp = map.get(impBean.getId());
            if (imp != null) {
                map.remove(impBean.getId());
                imp.setUpdatedBy(bean.getUser());
                imp.setUpdationDate(new Date());
            } else {
                imp = new Imp();
                imp.setCreatedBy(bean.getUser());
                imp.setCreationDate(new Date());
                imp.setAudits(audits);
                list.add(imp);
            }

            imp.setImpaction(impBean.getAction());
            imp.setImpduedate(impBean.getDuedate());
            imp.setImpreceived(impBean.getReceived());
            imp.setImpperformed(impBean.getPerformed());
            imp.setImpclosedtofile(impBean.getClosedtofile());
        }

        for (Imp item : map.values()) {
            list.remove(item);
        }

        impService.deleteAll(map.values());
    }
}
